package com.phkj.qdh.ui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.phkj.qdh.databinding.ActivityMainBinding
import com.phkj.qdh.ui.fragment.HomeHostFragment
import com.phkj.qdh.ui.fragment.instructionsheet.InstructionSheetHostFragment
import com.phkj.qdh.ui.fragment.user.UserHostFragment

class MainActivity : AppCompatActivity() {

    private lateinit var binding:ActivityMainBinding
    private val homeHostFragment: HomeHostFragment = HomeHostFragment.newInstance()
    private val instructionSheetHostFragment: InstructionSheetHostFragment = InstructionSheetHostFragment.newInstance()
    private val userHostFragment: UserHostFragment = UserHostFragment.newInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        supportFragmentManager.commit {
            add(binding.fcvMainAct.id,userHostFragment,"userHostFragment")
            add(binding.fcvMainAct.id,instructionSheetHostFragment,"instructionSheetHostFragment")
            add(binding.fcvMainAct.id,homeHostFragment,"homeHostFragment")
        }

        binding.rgMain.setOnCheckedChangeListener { _, id ->
            when (id) {
                binding.rbHome.id -> {
                    supportFragmentManager.commit {
                        show(homeHostFragment)
                        hide(instructionSheetHostFragment)
                        hide(userHostFragment)
                    }
                }
                binding.rbInstructionSheet.id -> {
                    supportFragmentManager.commit {
                        hide(homeHostFragment)
                        show(instructionSheetHostFragment)
                        hide(userHostFragment)
                    }
                }
                else -> {
                    supportFragmentManager.commit {
                        hide(homeHostFragment)
                        hide(instructionSheetHostFragment)
                        show(userHostFragment)
                        userHostFragment.show()
                    }
                }
            }

        }

        binding.rbHome.isChecked = true

        XXPermissions.with(this)
            .permission(Permission.READ_MEDIA_IMAGES,
                Permission.CALL_PHONE,
                Permission.POST_NOTIFICATIONS)
                .request(object :OnPermissionCallback{
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {

                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    super.onDenied(permissions, doNotAskAgain)

                }
            })

       /* lifecycleScope.launch {
            RxHttp.get(APP_LOGIN)
                .add("idCard","332522199603126666")
                .toFlowResponse<String>()
                .catch { cause ->
                    ToastUtils.show(cause.message)
                }
                .collectLatest {

                }
        }*/

    }


}